package com.dhecp.project.base.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.dhecp.common.core.domain.AjaxResult;
import com.dhecp.common.utils.MessageUtils;
import com.dhecp.common.utils.RandomValueUtils;
import com.dhecp.common.utils.StringUtils;
import com.dhecp.core.constant.DataFiltering;
import com.dhecp.core.utils.PageUtils;
import com.dhecp.framework.util.ShiroUtils;
import com.dhecp.project.base.domain.LogsticsServer;
import com.dhecp.project.base.mapper.LogsticsServerMapper;
import com.dhecp.project.base.service.LogsticsServerService;
import com.dhecp.project.common.service.impl.BaseModuleServiceImpl;
import com.dhecp.project.system.service.SysDictionaryForFieldService;

/**
 * @program: kjecp
 * @description: 公积金缴纳申请-公积金缴纳数据确认 服务层
 * @author: hlf
 * @create: 2022-05-09 09:58
 */
@Service
public class LogsticsServerServiceImpl extends BaseModuleServiceImpl implements LogsticsServerService {

    @Autowired
    private LogsticsServerMapper logsticsServerMapper;

    @Autowired
    private SysDictionaryForFieldService dictionaryForFieldService;

//    /**
//     *@Description: 查询
//     *@Param: [params]
//     *@return: java.util.List<com.dhecp.project.hr.domain.UserPaymentGJJ>
//     *@Author: hlf
//     *@date: 2022-05-09 10:10
//     */
//    @Override
//    public List<LogsticsServer> queryList(Map<String, Object> params) throws Exception {
//        params.put(DataFiltering.TABLE_ALIAS, "t1");	//查询表别名
//        params.put(DataFiltering.DATA_WHERE, dictionaryForFieldService.getWhereStr(params));	//常规查询条件SQL语句
//        params.put(DataFiltering.DATA_SCODE, dictionaryForFieldService.getDataScode(params));	//数据权限查询条件SQL语句
//        PageUtils.startPageStronger();
//        return logsticsServerMapper.select(params);
//    }

	/**
     * 查询物流服务商
	 * @param params
     * @Author: Jason
     * @date: 2022-09-02 21:40
	 */
	@Override
	public List<LogsticsServer> queryList(Map<String, Object> params) throws Exception{
        params.put(DataFiltering.TABLE_ALIAS, "t1");	//查询表别名
        params.put(DataFiltering.DATA_WHERE, dictionaryForFieldService.getWhereStr(params));	//常规查询条件SQL语句
//        params.put(DataFiltering.DATA_SCODE, dictionaryForFieldService.getDataScode(params));	//数据权限查询条件SQL语句

		if (params.get("field") == null || params.get("field") == ""){
			String orderBy = "name asc";
			PageUtils.startPageOrder(orderBy);
		}else{
			PageUtils.startPage();
		}
		
		return logsticsServerMapper.findLogsticsServer(params);
	}


    /**
     * @Description: 查询指定单nid的首记录
     * @Param: [params]
     * @Author: Jason
     * @date: 2022-09-02 21:40
     */
    @Override
    public LogsticsServer queryFirstByGuid(Map<String, Object> params) throws Exception {
        params.put(DataFiltering.TABLE_ALIAS, "t1");	//查询表别名
        params.put(DataFiltering.DATA_SCODE, dictionaryForFieldService.getDataScode(params));	//数据权限查询条件SQL语句
        return logsticsServerMapper.queryFirstByGuid(params);
    }
    
    /**
	 * 新增物流服务商
     * @author Jason
     * @date: 2022-09-02 21:40
     */
	@Override
	public AjaxResult insertLogsticsServer(Map<String, Object> params) {
		if (StringUtils.isEmpty(params)) {
			return AjaxResult.error("数据提交失败，必填参数有误！");
		}

		LogsticsServer entity = JSON.parseObject(JSON.toJSONString(params), LogsticsServer.class);
		entity.setGuid(RandomValueUtils.getGUID());
		entity.setCreateUser(ShiroUtils.getUserId());
		entity.setCreateTime(new Date());
		entity.setUpdateUser(ShiroUtils.getUserId());
		entity.setUpdateTime(new Date());
		if (entity.getGuid() == null || "".equals(entity.getGuid())){
			entity.setGuid(RandomValueUtils.getGUID());
		}
		
		if (entity.getCode() == null || entity.getCode().length() == 0) {
			return AjaxResult.error("数据提交失败，必填参数有误！");
		}
		
		logsticsServerMapper.insertLogsticsServer(entity);
		return AjaxResult.success(MessageUtils.message("module.success.issuccess"),entity.getGuid());
	}
    
    /**
	 * 修改物流服务商
     * @author Jason
     * @date: 2022-09-02 21:40
     */
	@Override
	public AjaxResult updateLogsticsServer(Map<String, Object> params) {
		if (StringUtils.isEmpty(params)) {
			return AjaxResult.error("数据提交失败，必填参数有误！");
		}

		LogsticsServer entity = JSON.parseObject(JSON.toJSONString(params), LogsticsServer.class);
		entity.setUpdateUser(ShiroUtils.getUserId());
		entity.setUpdateTime(new Date());
		
		if (entity.getCode() == null || entity.getCode().length() == 0) {
			return AjaxResult.error("数据提交失败，必填参数有误！");
		}
		
		logsticsServerMapper.updateByGuid(entity);
		return AjaxResult.success(MessageUtils.message("module.success.issuccess"),entity.getGuid());
	}

	/**
	 * 删除物流名称
	 * @author fxz
	 * @date: 2022-09-22
	 */
	@Override
	public AjaxResult deleteLogsticsServer(Map<String, Object> params) {
		List<LogsticsServer> logsticsServerList = JSON.parseArray(params.get("infoItems").toString(), LogsticsServer.class);
		if (logsticsServerList == null || logsticsServerList.size() == 0) {
			return AjaxResult.error(MessageUtils.message("module.error.jsontoentyerror"));
		}
		for (LogsticsServer logsticsServer : logsticsServerList) {
			logsticsServerMapper.deleteByGuid(logsticsServer);
		}
		return AjaxResult.success(MessageUtils.message("module.success.issuccess"));
	}
}